﻿Imports System.IO
Imports System.Text
Imports Model.Project
Imports Model.Midline

Public Class FILEHelper
    Public Shared Sub createFile(ByRef newpro As MyProject)
        Dim pubpath = newpro.ProDir + "\" + newpro.ProID + newpro.ProName
        If newpro.ProPts.Count > 0 Then
            For i As Integer = 0 To newpro.ProPts.Count - 1
                FileCopy(newpro.ProPts(i), pubpath + "\pointcloud\" + Path.GetFileName(newpro.ProPts(i)))
            Next
        End If
        '写平曲线数据到文件
        Dim sw As StreamWriter = New StreamWriter(pubpath + "\midline\隧道中线（平）.txt", True, ASCIIEncoding.UTF8)  'true是指以追加的方式打开指定文件  
        For i As Integer = 0 To newpro.ProMidLine.Count - 1
            Dim tempstring = newpro.ProMidLine(i).id.ToString() + ";"
            tempstring += newpro.ProMidLine(i).LineType.ToString() + ";"
            tempstring += newpro.ProMidLine(i).startpt.p_x.ToString() + ","
            tempstring += newpro.ProMidLine(i).startpt.p_y.ToString() + ";"
            tempstring += newpro.ProMidLine(i).startm.ToString() + ";"
            tempstring += newpro.ProMidLine(i).endpt.p_x.ToString() + ","
            tempstring += newpro.ProMidLine(i).endpt.p_y.ToString() + ";"
            tempstring += newpro.ProMidLine(i).endm.ToString() + ";"
            tempstring += newpro.ProMidLine(i).length.ToString() + ";"
            tempstring += newpro.ProMidLine(i).isturnright.ToString() + ";"
            tempstring += newpro.ProMidLine(i).midpt.p_x.ToString() + ","
            tempstring += newpro.ProMidLine(i).midpt.p_y.ToString() + ";"
            tempstring += newpro.ProMidLine(i).midm.ToString() + ";"
            tempstring += newpro.ProMidLine(i).radius.ToString() + ";"
            tempstring += newpro.ProMidLine(i).startRadius.ToString() + ";"
            tempstring += newpro.ProMidLine(i).endRadius.ToString() + ";"
            tempstring += newpro.ProMidLine(i).startoffset.ToString() + ";"
            tempstring += newpro.ProMidLine(i).endoffset.ToString()
            sw.WriteLine(tempstring)
            sw.Flush()
        Next
        sw.Close()
        sw = Nothing

        '写竖曲线数据到文件
        Dim swv As StreamWriter = New StreamWriter(pubpath + "\midline\隧道中线（竖）.txt", True, ASCIIEncoding.UTF8)
        swv.WriteLine(newpro.VLine.startm.ToString())
        swv.Flush()
        For i As Integer = 0 To newpro.VLine.bpdlist.Count - 1
            Dim tempstring = newpro.VLine.bpdlist(i).R.ToString() + ","
            tempstring += newpro.VLine.bpdlist(i).i1.ToString() + ","
            tempstring += newpro.VLine.bpdlist(i).i2.ToString() + ","
            tempstring += newpro.VLine.bpdlist(i).bpdm.ToString() + ","
            tempstring += newpro.VLine.bpdlist(i).elejd.ToString()
            swv.WriteLine(tempstring)
            swv.Flush()
        Next
        swv.Close()
        swv = Nothing
    End Sub
    ''' <summary>
    ''' 增加点云文件
    ''' </summary>
    ''' <param name="filepath"></param>
    ''' <param name="despath"></param>
    ''' <remarks></remarks>
    Public Shared Sub addptfile(ByRef filepath As String, ByRef despath As String)
        FileCopy(filepath, despath + "\pointcloud\" + Path.GetFileName(filepath))
    End Sub
    ''' <summary>
    ''' 读取中线文件
    ''' </summary>
    ''' <param name="currpro"></param>
    ''' <remarks></remarks>
    Public Shared Sub readFILE(ByVal currpro As MyProject)
        Dim pubpath = currpro.ProDir + "\" + currpro.ProID + currpro.ProName + "\midline\隧道中线（平）.txt"
        Dim reader As TextReader = File.OpenText(pubpath)
        Dim line As String = reader.ReadLine()
        Dim midl As midline
        currpro.ProMidLine.Clear()
        Do
            Dim mlml = line.Split(";")
            midl = New midline()
            midl.id = Integer.Parse(mlml(0))
            If (mlml(1) = "TypeLINE") Then
                midl.LineType = LineTypeList.TypeLINE
            ElseIf mlml(1) = "TypeCIRCLE" Then
                midl.LineType = LineTypeList.TypeCIRCLE
            Else
                midl.LineType = LineTypeList.TypeSUPERCIRCLE
            End If
            midl.startpt.p_x = Double.Parse(mlml(2).Split(",")(0))
            midl.startpt.p_y = Double.Parse(mlml(2).Split(",")(1))
            midl.startm = Double.Parse(mlml(3))
            midl.endpt.p_x = Double.Parse(mlml(4).Split(",")(0))
            midl.endpt.p_y = Double.Parse(mlml(4).Split(",")(1))
            midl.endm = Double.Parse(mlml(5))
            midl.length = Double.Parse(mlml(6))
            midl.isturnright = Boolean.Parse(mlml(7))
            midl.midpt.p_x = Double.Parse(mlml(8).Split(",")(0))
            midl.midpt.p_y = Double.Parse(mlml(8).Split(",")(1))
            midl.midm = Double.Parse(mlml(9))
            midl.radius = Double.Parse(mlml(10))
            midl.startRadius = Double.Parse(mlml(11))
            midl.endRadius = Double.Parse(mlml(12))
            midl.startoffset = Double.Parse(mlml(13))
            midl.endoffset = Double.Parse(mlml(14))
            currpro.ProMidLine.Add(midl)
            line = reader.ReadLine()
        Loop Until line = ""
        reader.Close()
        '下面是读取竖曲线参数代码，待添加
        Dim pubpathv = currpro.ProDir + "\" + currpro.ProID + currpro.ProName + "\midline\隧道中线（竖）.txt"
        Dim readerv As TextReader = File.OpenText(pubpathv)
        currpro.VLine.startm = Double.Parse(readerv.ReadLine())
        Dim linev As String = readerv.ReadLine()
        Dim bpd As bpd
        currpro.VLine.bpdlist.Clear()
        Do
            Dim splitbpd = linev.Split(",")
            bpd = New bpd()
            bpd.R = Double.Parse(splitbpd(0))
            bpd.i1 = Double.Parse(splitbpd(1))
            bpd.i2 = Double.Parse(splitbpd(2))
            bpd.bpdm = Double.Parse(splitbpd(3))
            bpd.elejd = Double.Parse(splitbpd(4))
            currpro.VLine.bpdlist.Add(bpd)
            linev = readerv.ReadLine()
        Loop Until linev = ""
    End Sub

    ''' <summary>
    ''' 读取里程列表文件
    ''' </summary>
    ''' <param name="filename"></param>
    ''' <remarks></remarks>
    Public Shared Function readMIL(ByVal filename As String) As List(Of Double)
        Dim reader As TextReader = File.OpenText(filename)
        Dim line As String = reader.ReadLine()
        Dim MilList As New List(Of Double)
        MilList.Clear()
        Do
            MilList.Add(Double.Parse(line))
            line = reader.ReadLine()
        Loop Until line = ""
        reader.Close()
        Return MilList
    End Function
End Class

